/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.thienloc.omap.controller;

import com.thienloc.omap.controller.scatchcard.TransactionQueryObject;
import com.thienloc.omap.entity.Partner;
import com.thienloc.omap.entity.Transaction;
import com.thienloc.omap.session.PartnersFacade;
import com.thienloc.omap.session.TransactionFacade;
import com.thienloc.omap.util.JsfUtil;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author GiangLT
 */
@ManagedBean
@ViewScoped
public class TransactionQueryBean implements Serializable {

    @EJB
    private TransactionFacade transactionFacade;
    @EJB
    private PartnersFacade partnersFacade;
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    private List<Transaction> list = new ArrayList<Transaction>();
    private TransactionQueryObject queryObject;

    @PostConstruct
    public void init() {
        try {
            queryObject = new TransactionQueryObject();
        } catch (Exception ex) {
            logger.error("", ex);
            JsfUtil.addErrorMessage("Error", ex.getMessage());
        }
    }

    public void query() {
        try {
            if (queryObject.getAccId() != null && !queryObject.getAccId().isEmpty()) {
                Partner partner = partnersFacade.find(queryObject.getAccId());
                queryObject.setPartner(partner);
            }
            this.list = transactionFacade.find(queryObject);
        } catch (Exception ex) {
            logger.error("", ex);
            JsfUtil.addErrorMessage("Error", ex.getMessage());
        }
    }

    public void confirmSuccessTransaction(Transaction transaction) {
        try {
            transaction.setTransErr("01");
            transaction.setTransErrDetail(TransactionError.errorMap.get("01"));
            transactionFacade.edit(transaction);
            JsfUtil.addSuccessMessage("Thành công", "Đã xác nhận thành công mã giao dịch " + transaction.getTransactionPK().getTransId());
        } catch (Exception ex) {
            logger.error("", ex);
            JsfUtil.addErrorMessage("Error", ex.getMessage());
        }
    }

    public TransactionQueryObject getQueryObject() {
        return queryObject;
    }

    public void setQueryObject(TransactionQueryObject queryObject) {
        this.queryObject = queryObject;
    }

    public List<Transaction> getList() {
        return list;
    }
}
